package com.shushan.invitecode.bean;

import com.android.net.bean.NetBaseBean;
import com.shushan.util.ToolUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: InviteBean
 * @Desciption: 万年历bean
 * @author: yichaohua
 * @date: 2018-09-15
 */
public class InviteBean extends NetBaseBean {

    public int all_num;//总签到天数
    public int continuity_num;//连续签到天数
    public int lack_id;//需补签天数
    public int year;
    public int month;
    public int patch_card;//补签卡数量
    private List<PatchBean> patchList = new ArrayList<PatchBean>();
    private List<DayBean> dayList = new ArrayList<DayBean>();
    public int startYear = 0;
    public int startMonth = 0;
    public int startDay = 0;

    @Override
    public void initByJson(JSONObject jsonObject) throws JSONException {
        all_num = jsonObject.optInt("all_num",0);
        continuity_num = jsonObject.optInt("continuity_num",0);
        lack_id = jsonObject.optInt("lack_id",0);
        year = jsonObject.optInt("year",0);
        month = jsonObject.optInt("month",0);

        JSONObject inviteTimeJson = jsonObject.optJSONObject("invite_time");
        if(inviteTimeJson != null){
            startYear = inviteTimeJson.optInt("year",0);
            startMonth = inviteTimeJson.optInt("month",0);
            startDay = inviteTimeJson.optInt("day",0);
        }

        JSONObject patchJson = jsonObject.optJSONObject("patch");
        if(patchJson != null){
            patch_card = patchJson.optInt("patch_card",0);
            JSONArray jsonArray = patchJson.optJSONArray("patch_list");
            if(jsonArray != null){
                for (int i = 0;i < jsonArray.length();i++){
                    JSONObject object = jsonArray.optJSONObject(i);
                    PatchBean patchBean = new PatchBean();
                    patchBean.initByJson(object);
                    patchList.add(patchBean);
                }

            }
        }

        JSONArray jsonArray = jsonObject.optJSONArray("days_list");
        if(jsonArray != null){
            for (int i = 0;i < jsonArray.length();i++){
                JSONObject object = jsonArray.optJSONObject(i);
                DayBean dayBean = new DayBean();
                dayBean.initByJson(object);
                dayList.add(dayBean);
            }

        }
    }

    public int getValidePatchCount(){
        if(ToolUtils.isListEmpty(patchList)){
            return 0;
        }

        int num = 0;
        for (PatchBean patchBean : patchList){
            if(patchBean.is_use == 0){
                num++;
            }
        }

        return num;
    }

    public PatchBean getValidePatch(){
        if(ToolUtils.isListEmpty(patchList)){
            return null;
        }

        for (PatchBean patchBean : patchList){
            if(patchBean.is_use == 0){
                return patchBean;
            }
        }

        return null;
    }

    public DayBean getDayBean(int day){
        if(ToolUtils.isListEmpty(dayList)){
            return null;
        }

        for (DayBean dayBean : dayList){
            if(dayBean.day == day){
                return dayBean;
            }
        }

        return null;
    }

    public int getNum(int day){
        if(ToolUtils.isListEmpty(dayList)){
            return 0;
        }

        for (DayBean dayBean : dayList){
            if(dayBean.day == day){
                return dayBean.num;
            }
        }

        return 0;
    }

    public List<PatchBean> getPatchList() {
        return patchList;
    }

    public void setPatchList(List<PatchBean> patchList) {
        this.patchList = patchList;
    }

    public List<DayBean> getDayList() {
        return dayList;
    }

    public void setDayList(List<DayBean> dayList) {
        this.dayList = dayList;
    }
}
